﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Report
{
    public partial class Main : Form
    {
       
        private ReportManager report;
        public Main()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            report = new ReportManager();
            var root = DbProvider.Data;
            var node = new TreeNode();
            report.InitializeDatawithReport(node, root,treeView1);
            treeView1.ExpandAll();
            radioButtonCost.Checked = true;
        }

      

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            mulutextBox.Text = treeView1.SelectedNode != null ?treeView1.SelectedNode.Text:string.Empty;
            mululabel.Text = mulutextBox.Text;
        }

        private void btnCreatedata_Click(object sender, EventArgs e)
        {
            var item = DbProvider.Data.GetbyName(mulutextBox.Text);
            var data = new Leaf()
                {
                    AddDate = dateTimePicker3.Value.Date,
                    Name = textBoxDetail.Text,
                    ContentSummary = textBoxDetail.Text,
                    Comment = commenttextBox.Text,
                    Borrow = string.IsNullOrEmpty(borrowtextBox.Text)?0:float.Parse(borrowtextBox.Text),
                    Lent = string.IsNullOrEmpty(lenttextBox.Text)?0:float.Parse(lenttextBox.Text)

                };

            if (radioButtonCost.Checked)
            {
                data.Cost = float.Parse(valuetextBox.Text);
            }
            else
            {
                data.Income = float.Parse(valuetextBox.Text);
            }
            if (DbProvider.Data.GetChildern().Any(r=>r.Name == item.Name))
            {
                data.FirstLevel = item.Name;
            }
            else
            {
                data.SecondLevel = item.Name;
            }
            item.Add(data);

            displaylabel.Text = textBoxDetail.Text +"添加成功！";


        }

        private void clearbutton_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode = null;
            mulutextBox.Text = mululabel.Text = string.Empty;
        }

        private void createmuluButton_Click(object sender, EventArgs e)
        {
            if (treeView1.Nodes.Count == 0 || treeView1.SelectedNode == null)
            {
                var com = new Composite();
                com.Name = textBox1.Text;
                com.FirstLevel = com.Name;
                DbProvider.Data.Add(com);
                var node1 = new TreeNode();

                report.InitializeDatawithReport(node1, DbProvider.Data, treeView1);

                return;
            }
            var item = DbProvider.Data.GetbyName(treeView1.SelectedNode.Text);
            if (item == null)
            {
                MessageBox.Show("请选中一个父目录");
                return;
            }
            else
            {
                var newItem = new Composite();
                newItem.Name = textBox1.Text;
                newItem.SecondLevel = newItem.Name;
                newItem.FirstLevel = item.Name;
                item.Add(newItem);
            }
            var node = new TreeNode();
            treeView1.Nodes.Clear();
            report.InitializeDatawithReport(node, DbProvider.Data, treeView1);
        }

        private void updatemulubutton_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                MessageBox.Show("请选中一个目录");
                return;
            }
            var item = DbProvider.Data.GetbyName(treeView1.SelectedNode.Text);
           
            item.Name = textBox1.Text;
            treeView1.SelectedNode.Text = item.Name;
            treeView1.Update();
            treeView1.Refresh();
        }

        private void viewdetailsbutton_Click(object sender, EventArgs e)
        {
            var form = new ListForm();
            form.ShowDialog();
        }

        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            report.SaveToDb();
        }

        private void buttonReport_Click(object sender, EventArgs e)
        {
            var reportForm = new ReportForm();

            reportForm.ShowDialog();
        }

        private void queryButton_Click(object sender, EventArgs e)
        {
            var from = fromdateTimePicker.Value.Date;
            var to = todateTimePicker.Value.Date;

           var results = report.BuildReportList(DbProvider.Data, string.Empty).Where(r=>r.AddDate >= from && r.AddDate <= to).ToList();
            var reportForm = new ReportForm(results);
            reportForm.ShowDialog();
        }

        private void deletebutton_Click(object sender, EventArgs e)
        {
            var selectedNode = treeView1.SelectedNode;
            if (selectedNode  == null)
            {
                MessageBox.Show("无法找到目录");
                return;
            }
            else
            {
               
               if (selectedNode.Parent == null)
               {
                   var item = DbProvider.Data.GetbyName(selectedNode.Text);

                   DbProvider.Data.Remove(item);
               }
               else
               {
                   var parenttext = selectedNode.Parent.Text;
                   var parent = DbProvider.Data.GetbyName(parenttext);
                   var item = DbProvider.Data.GetbyName(selectedNode.Text);

                   parent.Remove(item);

               }
               treeView1.Nodes.Remove(selectedNode);
            }

        }

        private void savebutton_Click(object sender, EventArgs e)
        {
            report.SaveToDb();
        }

        private void contentQuerybutton_Click(object sender, EventArgs e)
        {
            var list = report.BuildDetailList();
            var results = list.Where(r => r.ContentSummary.Contains(queryContenttextBox.Text) ||
                                          r.FirstLevel.Contains(queryContenttextBox.Text) ||
                                          r.SecondLevel.Contains(queryContenttextBox.Text));
            var reportForm = new ListForm(results.ToList());
            reportForm.ShowDialog();
        }

      
    }
}
